﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EmoneyWeiXinCommon;
using EmoneyWeiXinBLL;
using EmoneyWeiXinEntity;
using System.IO;
using System.Data;
using EmoneyWeiXin.Filter;
namespace EmoneyWeiXin.Controllers
{
    public class NewsModelController : Controller
    {
        //
        // GET: /NewsModel/

        public ActionResult Index()
        {
            return View();
        }

        //NewsModelList页面
          [LoginAuthorizeAttribute]
        public ActionResult NewsModelList()
        {
            ViewBag.leftid = int.Parse(Request["leftid"]);
            return View();
        }
        public string UpdateNewsModel()
        {
            var uid = 0;
            UserEntity us = LoginUserInfo.GetLoginUserCache();
            if (us != null)
                uid = us.id;
            int weixinid = int.Parse(Request["weixinid"]);
            int iRe = 0;
            var flag = Request["op"].Trim();//flag=-1表示删除
            var id = int.Parse(Request["id"].Trim());//ID=0表示新增，否则表示修改
            if (flag != "-1")
            {
                var modelid = Request["modelid"];
                var title = Request["title"];
                var content = Request["content"];
                var memo = Request["memo"];
                WeixinNewsModelEntity newsmodel = new WeixinNewsModelEntity();
                newsmodel.id = id;
                newsmodel.modelid = modelid;
                newsmodel.title = title;
                newsmodel.content = content;
                newsmodel.remark = memo;
                newsmodel.creator = uid;
                newsmodel.weixinid = weixinid;
                newsmodel.isvalid = 1;
                iRe = NewsModelManager.UpdateNewsModel(newsmodel);
            }
            else
            {
                iRe = NewsModelManager.DeleteNewsModel(id);

            }
            if (iRe > 0)
            {
                return "1";
            }
            else
            {
                return "-1";
            }
        }
        public string QueryNewsModel()
        {
            var weixinid = int.Parse(Request["weixinid"]);
            var pindex = int.Parse(Request["pindex"]);
            var psize = int.Parse(Request["psize"]);
            int total = 0;
            // var jsonString = "";
            IList<WeixinNewsModelEntity> data = NewsModelManager.getNewsModelList(weixinid, pindex, psize, out total);
            //if (data != null && data.Count > 0)
            //{
            //    jsonString = JsonTools.ToJson(data);
            //}
            //return total.ToString() + "~" + jsonString;
            return JsonTools.ToJson(new { total = total, list = data });

        }

        //NewsModelFieldList页面
        public ActionResult NewsModelFieldList()
        {
            ViewBag.leftid = int.Parse(Request["leftid"]);
            //Session["uid"] = 1;

            // ViewBag.modelid = Server.UrlDecode(Request["modelid"].Trim());
            var id = int.Parse(Request["mid"].Trim());
            //ViewBag.title = Server.UrlDecode(Request["title"].Trim());
            //ViewBag.content = Server.UrlDecode(Request["content"].Trim());
            //ViewBag.memo = Server.UrlDecode(Request["memo"].Trim());
            ViewBag.weixinid = Server.UrlDecode(Request["weixinid"].Trim());
            var obj = NewsModelManager.getNewsModelListByID(id);
            ViewBag.NewsModel = obj;
            return View();
        }
        public string GetNewsModelByID()
        {
            var id = int.Parse(Request["id"].Trim());
            var obj = NewsModelManager.getNewsModelListByID(id);
            if (obj != null)
                return JsonTools.ToJson(obj);
            else
                return "";
        }
        public string UpdateNewsModelField()
        {
            var uid = 0;
            UserEntity us = LoginUserInfo.GetLoginUserCache();
            if (us != null)
                uid = us.id;
            //int weixinid = int.Parse(Request["weixinid"]);
            int iRe = 0;
            var flag = Request["op"].Trim();//flag=-1表示删除
            var id = int.Parse(Request["id"].Trim());//ID=0表示新增，否则表示修改
            if (flag != "-1")
            {
                var mid = int.Parse(Request["mid"]);
                var fieldname = Request["fieldname"];
                var fieldvalue = Request["fieldvalue"];
                var style = Request["style"];
                var memo = Request["memo"];
                WeixinNewsModelSettingEntity newsmodel = new WeixinNewsModelSettingEntity();
                newsmodel.id = id;
                newsmodel.mid = mid;
                newsmodel.fieldname = fieldname;
                newsmodel.fieldvalue = fieldvalue;
                newsmodel.style = style;
                newsmodel.remark = memo;
                newsmodel.creator = uid;
                //newsmodel.weixinid = weixinid;
                newsmodel.isvalid = 1;
                iRe = NewsModelManager.UpdateNewsModelField(newsmodel);
            }
            else
            {
                iRe = NewsModelManager.DeleteNewsFieldModel(id, 0);
            }
            if (iRe > 0)
            {
                return "1";
            }
            else
            {
                return "-1";
            }
        }
        public string QueryNewsModelField()
        {
            var mid = int.Parse(Request["mid"]);
            var pindex = int.Parse(Request["pindex"]);
            var psize = int.Parse(Request["psize"]);
            int total = 0;
            var jsonString = "";
            IList<WeixinNewsModelSettingEntity> data = NewsModelManager.getNewsModelFieldList(mid, pindex, psize, out total);
            if (data != null && data.Count > 0)
            {
                jsonString = JsonTools.ToJson(data);
            }
            return total.ToString() + "^" + jsonString;
        }
    }
}
